<?php

namespace App\Console\Commands;

use App\Console\Base;
use App\Models\Game;
use common\enum\CommonEnum;
use Illuminate\Support\Carbon;
use App\Models\GameTag;
use App\Models\GameTagRelation;
use App\Models\GameRecommend;

class CheckGame extends Base
{
    /**
     * 控制台命令 signature 的名称。
     *
     * @var string
     */
    protected $signature = self::PREFIX . 'check_game {--type=}';


    /**
     * 控制台命令说明。
     *
     * @var string
     */
    protected $description = 'CheckGame';

    /**
     * 执行控制台命令。
     *
     * @return mixed
     */
    public function handle()
    {
        $type = $this->option('type');
        switch ($type) {
            case 'tags':
                // 热门，最新
                $this->updateTags(CommonEnum::GAME_EGAME, '热门');
                $this->updateTags(CommonEnum::GAME_EGAME, '最新');
                $this->updateTags(CommonEnum::GAME_CARD, '热门');
                $this->updateTags(CommonEnum::GAME_CARD, '最新');
                break;
            case 'recommend':
                $this->updateRecommend();
                break;
        }
    }

    public function updateTags($gameType, $tagName)
    {
        $column = $tagName == '热门' ? 'hot_end_time' : 'new_end_time';
        $tag = GameTag::where(['game_type' => $gameType, 'name' => $tagName])->first();
        if (!$tag) {
            $this->error("Tag Not found!");
            return;
        }
        $now = Carbon::now()->toDateTimeString();
        $page = 1;
        $size = 1000;
        $games = [];
        while (count($games) == $size || $page === 1) {
            $games = Game::where(['game_type' => $gameType])->whereNotNull($column)->offset(($page - 1) * $size)->limit($size)->get();
            foreach ($games as $g) {
                // 去掉过期标签
                if ($g[$column] < $now) {
                    $g->update([$column => null]);
                    GameTagRelation::where(['tag_id' => $tag['id'], 'game_id' => $g['id']])->delete();
                }
                // 增加标签
                if ($g[$column] > $now) {
                    if (!GameTagRelation::where(['tag_id' => $tag['id'], 'game_id' => $g['id']])->first()) {
                        GameTagRelation::insert(['tag_id' => $tag['id'], 'game_id' => $g['id']]);
                    }
                }
            }
            $page++;
        }
    }

    public function updateRecommend()
    {
        $grs = GameRecommend::all();
        $now = Carbon::now()->toDateTimeString();
        foreach ($grs as $g) {
            if ($g['status'] == CommonEnum::ENABLED) {
                // 活动过期，停掉
                if ($g['end_time'] < $now) {
                    $g->update(['status' => CommonEnum::DISABLE]);
                }
            }

            if ($g['status'] == CommonEnum::DISABLE) {
                // 活动到时间了，启用
                if ($g['start_time'] < $now && $g['end_time'] > $now) {
                    $g->update(['status' => CommonEnum::ENABLED]);
                }
            }
        }
    }
}
